/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.spring.initilizer;

import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextLoader;

/**
 * {@link ContextLoader} that returns the generated spring configuration.
 */
public class PluginWebContextLoader
    implements ContextLoader
{

  private static final String LOCATION = PluginWebContextLoader.class.getCanonicalName();



  /**
   * <p>
   * Loads a new {@link ApplicationContext context} based on the supplied
   * <code>locations</code>, configures the context, and finally returns the
   * context, potentially <em>refreshed</em>.
   * </p>
   * <p>
   * Configuration locations are generally considered to be classpath resources
   * by default.
   * </p>
   * <p>
   * Concrete implementations should register annotation configuration
   * processors with bean factories of
   * {@link ApplicationContext application contexts} loaded by this
   * ContextLoader. Beans will therefore automatically be candidates for
   * annotation-based dependency injection using
   * {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
   * and {@link javax.annotation.Resource @Resource}.
   * </p>
   * <p>
   * Any ApplicationContext loaded by a ContextLoader <strong>must</strong>
   * register a JVM shutdown hook for itself. Unless the context gets closed
   * early, all context instances will be automatically closed on JVM shutdown.
   * This allows for freeing external resources held by beans within the
   * context, e.g. temporary files.
   * </p>
   * 
   * @param locations
   *          The resource locations to use to load the application context.
   * @return a new application context
   * @see org.springframework.test.context.ContextLoader#loadContext(java.lang.String[])
   */
  public ApplicationContext loadContext(String... locations)
      throws Exception
  {
    // ProcessWebInitializer.releaseApplicationContext();
    return ProcessWebInitializer.getWebApplicationContext();
  }



  /**
   * <p>
   * Processes application context resource locations for a specified class.
   * </p>
   * <p>
   * Concrete implementations may choose to modify the supplied locations,
   * generate new locations, or simply return the supplied locations unchanged.
   * </p>
   * 
   * @param clazz
   *          The class with which the locations are associated: used to
   *          determine how to process the supplied locations.
   * @param locations
   *          The unmodified locations to use for loading the application
   *          context; can be <code>null</code> or empty.
   * @return An array of application context resource locations.
   * @see org.springframework.test.context.ContextLoader#processLocations(java.lang.Class,
   *      java.lang.String[])
   */
  public String[] processLocations(Class<?> clazz, String... locations)
  {
    return new String[] { LOCATION };
  }

}
